<?php 
session_start();
ob_start();
?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php include ("ewconfig.php") ?>
<?php include ("db.php") ?>
<?php include ("bannerinfo.php") ?>
<?php include ("advsecu.php") ?>
<?php include ("phpmkrfn.php") ?>
<?php include ("ewupload.php") ?>
<?php

// Initialize common variables
$x_id = NULL;
$ox_id = NULL;
$z_id = NULL;
$ar_x_id = NULL;
$ari_x_id = NULL;
$x_idList = NULL;
$x_idChk = NULL;
$cbo_x_id_js = NULL;
$x_status = NULL;
$ox_status = NULL;
$z_status = NULL;
$ar_x_status = NULL;
$ari_x_status = NULL;
$x_statusList = NULL;
$x_statusChk = NULL;
$cbo_x_status_js = NULL;
$x_image = NULL;
$ox_image = NULL;
$z_image = NULL;
$fs_x_image = 0;
$fn_x_image = "";
$ct_x_image = "";
$wd_x_image = 0;
$ht_x_image = 0;
$a_x_image = "";
$ar_x_image = NULL;
$ari_x_image = NULL;
$x_imageList = NULL;
$x_imageChk = NULL;
$cbo_x_image_js = NULL;
$fs_x_image = 0;
$fn_x_image = "";
$ct_x_image = "";
$wd_x_image = 0;
$ht_x_image = 0;
$a_x_image = "";
$x_title = NULL;
$ox_title = NULL;
$z_title = NULL;
$ar_x_title = NULL;
$ari_x_title = NULL;
$x_titleList = NULL;
$x_titleChk = NULL;
$cbo_x_title_js = NULL;
$x_description = NULL;
$ox_description = NULL;
$z_description = NULL;
$ar_x_description = NULL;
$ari_x_description = NULL;
$x_descriptionList = NULL;
$x_descriptionChk = NULL;
$cbo_x_description_js = NULL;
$x_sort = NULL;
$ox_sort = NULL;
$z_sort = NULL;
$ar_x_sort = NULL;
$ari_x_sort = NULL;
$x_sortList = NULL;
$x_sortChk = NULL;
$cbo_x_sort_js = NULL;
$x_author_id = NULL;
$ox_author_id = NULL;
$z_author_id = NULL;
$ar_x_author_id = NULL;
$ari_x_author_id = NULL;
$x_author_idList = NULL;
$x_author_idChk = NULL;
$cbo_x_author_id_js = NULL;
$x_modified_id = NULL;
$ox_modified_id = NULL;
$z_modified_id = NULL;
$ar_x_modified_id = NULL;
$ari_x_modified_id = NULL;
$x_modified_idList = NULL;
$x_modified_idChk = NULL;
$cbo_x_modified_id_js = NULL;
$x_created_date = NULL;
$ox_created_date = NULL;
$z_created_date = NULL;
$ar_x_created_date = NULL;
$ari_x_created_date = NULL;
$x_created_dateList = NULL;
$x_created_dateChk = NULL;
$cbo_x_created_date_js = NULL;
$x_updated_date = NULL;
$ox_updated_date = NULL;
$z_updated_date = NULL;
$ar_x_updated_date = NULL;
$ari_x_updated_date = NULL;
$x_updated_dateList = NULL;
$x_updated_dateChk = NULL;
$cbo_x_updated_date_js = NULL;
?>
<?php

// Load key from QueryString
$bCopy = true;
$x_id = @$_GET["id"];
if (($x_id == "") || (is_null($x_id))) $bCopy = false;

// Get action
$sAction = @$_POST["a_add"];
if (($sAction == "") || ((is_null($sAction)))) {
	if ($bCopy) {
		$sAction = "C"; // Copy record
	} else {
		$sAction = "I"; // Display blank record
	}
} else {

	// Get fields from form
	$x_id = @$_POST["x_id"];
	$x_status = @$_POST["x_status"];
	$x_image = @$_POST["x_image"];
	$x_title = @$_POST["x_title"];
	$x_description = @$_POST["x_description"];
	$x_sort = @$_POST["x_sort"];
	$x_author_id = @$_POST["x_author_id"];
	$x_modified_id = @$_POST["x_modified_id"];
	$x_created_date = @$_POST["x_created_date"];
	$x_updated_date = @$_POST["x_updated_date"];
}
$conn = phpmkr_db_connect(HOST, USER, PASS, DB, PORT);
switch ($sAction) {
	case "C": // Copy record
		if (!LoadData($conn)) { // Load record
			$_SESSION[ewSessionMessage] = "No records found";
			phpmkr_db_close($conn);
			ob_end_clean();
			header("Location: bannerlist.php");
			exit();
		}
		break;
	case "A": // Add
		if (AddData($conn)) { // Add new record
			$_SESSION[ewSessionMessage] = "Add New Record Successful";
			phpmkr_db_close($conn);
			ob_end_clean();
			header("Location: bannerlist.php");
			exit();
		}
		break;
}
?>
<?php include ("header.php") ?>
<script type="text/javascript">
<!--
EW_LookupFn = "ewlookup.php"; // ewlookup file name
EW_AddOptFn = "ewaddopt.php"; // ewaddopt.php file name

//-->
</script>
<script type="text/javascript" src="ewp.js"></script>
<script type="text/javascript">
<!--
EW_dateSep = "-"; // set date separator
EW_UploadAllowedFileExt = "gif,jpg,jpeg,bmp,png"; // allowed upload file extension

//-->
</script>
<script type="text/javascript">
<!--
function EW_checkMyForm(EW_this) {
if (EW_this.x_status && !EW_hasValue(EW_this.x_status, "RADIO")) {
	if (!EW_onError(EW_this, EW_this.x_status, "RADIO", "Please enter required field - status"))
		return false;
}
if (EW_this.x_image && !EW_hasValue(EW_this.x_image, "FILE")) {
	if (!EW_onError(EW_this, EW_this.x_image, "FILE", "Please enter required field - Gambar"))
		return false;
}
if (EW_this.x_image && !EW_checkfiletype(EW_this.x_image.value)) { 
	if (!EW_onError(EW_this, EW_this.x_image, "FILE", "File type is not allowed.")) 
	return false; 
}
if (EW_this.x_title && !EW_hasValue(EW_this.x_title, "TEXT")) {
	if (!EW_onError(EW_this, EW_this.x_title, "TEXT", "Please enter required field - Judul"))
		return false;
}
return true;
}

//-->
</script>
<script type="text/javascript">
<!--
	var EW_DHTMLEditors = [];

//-->
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="admin_td_title">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td class="admin_title"><?=str_replace(","," &gt; ",$_SESSION['session_cms_title'])?> &raquo; Add to&nbsp;</td>
		   </tr>
		 </table>
 	</td>
  </tr>
</table>
<br>
<a href="bannerlist.php" class="button-modify">Back to List</a>
<br />
<form name="fbanneradd" id="fbanneradd" action="banneradd.php" method="post" enctype="multipart/form-data" onSubmit="return EW_checkMyForm(this);">
<p>
<input type="hidden" name="a_add" value="A">
<input type="hidden" name="EW_Max_File_Size" value="2000000">
<?php
if (@$_SESSION[ewSessionMessage] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[ewSessionMessage] ?></span></p>
<?php
	$_SESSION[ewSessionMessage] = ""; // Clear message
}
?>
<table class="ewTable">
	<tr>
		<td class="ewTableHeader"><span>status<span class='ewmsg'>&nbsp;*</span></span></td>
		<td class="ewTableAltRow"><span id="cb_x_status">
<?php if (!(!is_null($x_status)) || ($x_status == "")) { $x_status = "A";} // Set default value ?>
<?php echo RenderControl(1, 0, 5, 1); ?>
<input type="radio" name="x_status"<?php if (@$x_status == "A") { ?> checked<?php } ?> value="<?php echo htmlspecialchars("A"); ?>">
<?php echo get_icon_status("A");?>
<?php echo RenderControl(1, 0, 5, 2); ?>
<?php echo RenderControl(1, 1, 5, 1); ?>
<input type="radio" name="x_status"<?php if (@$x_status == "I") { ?> checked<?php } ?> value="<?php echo htmlspecialchars("I"); ?>">
<?php echo get_icon_status("I"); ?>
<?php echo RenderControl(1, 1, 5, 2); ?>
</span></td>
	</tr>
	<tr>
		<td class="ewTableHeader"><span>Gambar<span class='ewmsg'>&nbsp;*</span></span></td>
		<td class="ewTableAltRow"><span id="cb_x_image">
<?php $x_image = ""; // Clear BLOB related fields ?>
<input type="file" id="x_image" name="x_image" size="60">
</span></td>
	</tr>
	<tr>
		<td class="ewTableHeader"><span>Judul<span class='ewmsg'>&nbsp;*</span></span></td>
		<td class="ewTableAltRow"><span id="cb_x_title">
<input type="text" name="x_title" id="x_title" size="60" maxlength="255" value="<?php echo htmlspecialchars(@$x_title) ?>">
</span></td>
	</tr>
	<tr>
		<td class="ewTableHeader"><span>Deskripsi</span></td>
		<td class="ewTableAltRow"><span id="cb_x_description">
<textarea cols="100" rows="8" id="x_description" name="x_description" maxlength="500"><?php echo @$x_description; ?></textarea>
</span></td>
	</tr>
<?php $x_sort = 0 // Set default value ?>
<input type="hidden" id="x_sort" name="x_sort" value="<?php echo @$x_sort; ?>">
<?php $x_author_id = $SessionAuthorId // Set default value ?>
<input type="hidden" id="x_author_id" name="x_author_id" value="<?php echo @$x_author_id; ?>">
<?php $x_modified_id = $SessionAuthorId // Set default value ?>
<input type="hidden" id="x_modified_id" name="x_modified_id" value="<?php echo @$x_modified_id; ?>">
<?php $x_created_date = date('Y-m-d h:i:s') // Set default value ?>
<input type="hidden" id="x_created_date" name="x_created_date" value="<?php echo @$x_created_date; ?>">
<?php $x_updated_date = date('Y-m-d h:i:s') // Set default value ?>
<input type="hidden" id="x_updated_date" name="x_updated_date" value="<?php echo @$x_updated_date; ?>">
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="SAVE">
<input type="button" name="btnCancel" id="btnCancel" value="CANCEL" onclick="javascript:history.back(-1)" />
</form>
<?php include ("footer.php") ?>
<?php
phpmkr_db_close($conn);
?>
<?php

//-------------------------------------------------------------------------------
// Function LoadData
// - Variables setup: field variables

function LoadData($conn)
{
	global $x_id;
	$sFilter = ewSqlKeyWhere;
	if (!is_numeric($x_id)) return false;
	$x_id =  (get_magic_quotes_gpc()) ? stripslashes($x_id) : $x_id;
	$sFilter = str_replace("@id", AdjustSql($x_id), $sFilter); // Replace key value
	$sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sFilter, "");
	$rs = phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
	if (phpmkr_num_rows($rs) == 0) {
		$bLoadData = false;
	} else {
		$bLoadData = true;
		$row = phpmkr_fetch_array($rs);

		// Get the field contents
		$GLOBALS["x_id"] = $row["id"];
		$GLOBALS["x_status"] = $row["status"];
		$GLOBALS["x_image"] = $row["image"];
		$GLOBALS["x_title"] = $row["title"];
		$GLOBALS["x_description"] = $row["description"];
		$GLOBALS["x_sort"] = $row["sort"];
		$GLOBALS["x_author_id"] = $row["author_id"];
		$GLOBALS["x_modified_id"] = $row["modified_id"];
		$GLOBALS["x_created_date"] = $row["created_date"];
		$GLOBALS["x_updated_date"] = $row["updated_date"];
	}
	phpmkr_free_result($rs);
	return $bLoadData;
}
?>
<?php

//-------------------------------------------------------------------------------
// Function AddData
// - Add Data
// - Variables used: field variables

function AddData($conn)
{
	global $x_id;
	$sFilter = ewSqlKeyWhere;

	// Check for duplicate key
	$bCheckKey = true;
	if ((@$x_id == "") || (is_null(@$x_id))) {
		$bCheckKey = false;
	} else {
		$sFilter = str_replace("@id", AdjustSql($x_id), $sFilter); // Replace key value
	}
	if ($bCheckKey) {
		$sSqlChk = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sFilter, "");
		$rsChk = phpmkr_query($sSqlChk, $conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSqlChk);
		if (phpmkr_num_rows($rsChk) > 0) {
			$_SESSION[ewSessionMessage] = "Duplicate value for primary key";
			phpmkr_free_result($rsChk);
			return false;
		}
		phpmkr_free_result($rsChk);
	}
		$EW_MaxFileSize = @$_POST["EW_Max_File_Size"];

		// Check the file size
		if (!empty($_FILES["x_image"]["size"])) {
			if (!empty($EW_MaxFileSize) && $_FILES["x_image"]["size"] > $EW_MaxFileSize) {
				die(str_replace("%s", $EW_MaxFileSize, "Max. file size (%s bytes) exceeded."));
			}
		}
		$fn_x_image = $_FILES["x_image"]["name"];

		// Check the file type
		if (!empty($fn_x_image)) {
			if (!ewUploadAllowedFileExt($fn_x_image)) {
				die("File type is not allowed.");
			}
		}
		$ct_x_image = @$_POST["x_image"];
		$x_image = @$_POST[ "x_image"];
		$wd_x_image = @$_POST["wd_x_image"];
		$ht_x_image = @$_POST["ht_x_image"];
		$a_x_image =  @$_POST["a_x_image"];

	// Field status
	$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_status"]) : $GLOBALS["x_status"]; 
	$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
	$fieldList["`status`"] = $theValue;

	// Field image
		if (is_uploaded_file($_FILES["x_image"]["tmp_name"])) {
			$sTmpFolder = ewUploadPathEx(true, "../uploads/banner/");
			$theName = ewUploadFileNameEx($sTmpFolder, $_FILES["x_image"]["name"]);
			$destfile = $sTmpFolder . $theName;
			
			/* OLD MOVE FILE
			if (!move_uploaded_file($_FILES["x_image"]["tmp_name"], $destfile)) // Move file to destination path
				die("" . $destfile);
			@chmod($destfile, defined(EW_UploadedFileMode) ? EW_UploadedFileMode : 0666);
			*/
			
			list($width, $height, $type, $attr) = getimagesize($_FILES["x_image"]["tmp_name"]);
			
			if($width > 985){
			// Move & Resize Uploaded File
				$resize = new SimpleImage(); 
				$resize->load($_FILES["x_image"]["tmp_name"]); 
				$resize->resizeToWidth(985); 				
				$resize->save($destfile);
				@chmod($destfile, defined(EW_UploadedFileMode) ? EW_UploadedFileMode : 0666);
			}else{
				if (!move_uploaded_file($_FILES["x_image"]["tmp_name"], $destfile)) // Move file to destination path
					die("" . $destfile);	
				@chmod($destfile, defined(EW_UploadedFileMode) ? EW_UploadedFileMode : 0666);					
			}
			//Resize Image
				$resize2 = new SimpleImage();
				$resizeName=$theName;
				$resizePath="../../uploads/banner/thumbs/";
				$theResize=$resizePath.$resizeName;
				$resize2->load($destfile); 
				$resize2->resizeToWidth(300); 				
				$resize2->save($theResize);
				
			// File name
			$theName = (!get_magic_quotes_gpc()) ? addslashes($theName) : $theName;
			$fieldList["`image`"] = " '" . $theName . "'";
			@unlink($_FILES["x_image"]["tmp_name"]);
		}

	// Field title
	$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_title"]) : $GLOBALS["x_title"]; 
	$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
	$fieldList["`title`"] = $theValue;

	// Field description
	$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_description"]) : $GLOBALS["x_description"]; 
	$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
	$fieldList["`description`"] = $theValue;

	// Field sort
	$theValue = ($GLOBALS["x_sort"] != "") ? intval($GLOBALS["x_sort"]) : "NULL";
	$fieldList["`sort`"] = $theValue;

	// Field author_id
	$theValue = ($GLOBALS["x_author_id"] != "") ? intval($GLOBALS["x_author_id"]) : "NULL";
	$fieldList["`author_id`"] = $theValue;

	// Field modified_id
	$theValue = ($GLOBALS["x_modified_id"] != "") ? intval($GLOBALS["x_modified_id"]) : "NULL";
	$fieldList["`modified_id`"] = $theValue;

	// Field created_date
	$theValue = ($GLOBALS["x_created_date"] != "") ? " '" . ConvertDateToMysqlFormat($GLOBALS["x_created_date"]) . "'" : "Null";
	$fieldList["`created_date`"] = $theValue;

	// Field updated_date
	$theValue = ($GLOBALS["x_updated_date"] != "") ? " '" . ConvertDateToMysqlFormat($GLOBALS["x_updated_date"]) . "'" : "Null";
	$fieldList["`updated_date`"] = $theValue;

	// Inserting event
	if (Recordset_Inserting($fieldList)) {

		// Insert
		$sSql = "INSERT INTO `banner` (";
		$sSql .= implode(",", array_keys($fieldList));
		$sSql .= ") VALUES (";
		$sSql .= implode(",", array_values($fieldList));
		$sSql .= ")";	
		phpmkr_query($sSql, $conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
		$fieldList["`id`"] = phpmkr_insert_id($conn);
		$result = (phpmkr_affected_rows($conn) > 0);

		// Inserted event
		if ($result) Recordset_Inserted($fieldList);
	} else {
		$result = false;
	}
	return $result;
}

// Inserting event
function Recordset_Inserting(&$newrs)
{

	// Enter your customized codes here
	return true;
}

// Inserted event
function Recordset_Inserted($newrs)
{
	$table = "banner";
}
?>
